package com.breeze.components.core.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

/**
 * @author breeze
 */
@Slf4j
public class NormalRun implements InitializingBean, DisposableBean {

  private volatile boolean started = false;

  private volatile boolean shutdown = false;

  public void normalRun(Runnable runnable) {
    if (normalRun()) {
      runnable.run();
    }
  }

  public boolean normalRun() {
    return started && !shutdown;
  }

  @Override
  public void afterPropertiesSet() {
    this.started = true;
    this.shutdown = false;
  }

  @Override
  public void destroy() {
    this.started = false;
    this.shutdown = true;
  }
}
